﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.ComponentModel;
using System.Windows.Interop;
using System.Reflection;
using System.Globalization;
using System.Windows.Markup;

namespace SmallProgram.Controls
{
    /// <summary>
    /// Interaction logic for MessageBox.xaml
    /// </summary>
    public partial class MessageBox : Window, INotifyPropertyChanged
    {
        internal NotificationButtons selectedButton;
        private bool closeInvokedInternally;
        private HwndSource hwndSource;

        public event PropertyChangedEventHandler PropertyChanged;

        private string description;
        public string Description
        {
            get
            {
                return this.description;
            }
            set
            {
                this.description = value;
                this.RaisePropertyChangeEvent("Description");
            }
        }

        private object optionalContent;
        public object OptionalContent
        {
            get
            {
                return this.optionalContent;
            }
            set
            {
                this.optionalContent = value;
                this.RaisePropertyChangeEvent("OptionalContent");
            }
        }

        private object footer;
        public object Footer
        {
            get
            {
                return this.footer;
            }
            set
            {
                this.footer = value;
                this.RaisePropertyChangeEvent("Footer");
            }
        }

        private bool isCancelable = true;
        public bool IsCancelable
        {
            get
            {
                return this.isCancelable;
            }
            set
            {
                this.isCancelable = value;
                this.RaisePropertyChangeEvent("IsCancelable");
            }
        }

        private NotificationButtons notificationButtons = NotificationButtons.OK;
        public NotificationButtons NotificationButtons
        {
            get
            {
                return this.notificationButtons;
            }
            set
            {
                this.notificationButtons = value;
                this.RaisePropertyChangeEvent("NotificationButtons");
            }
        }

        private NotificationButtons defaultButton = NotificationButtons.OK;
        public NotificationButtons DefaultButton
        {
            get
            {
                return this.defaultButton;
            }
            set
            {
                this.defaultButton = value;
                this.RaisePropertyChangeEvent("DefaultButton");
            }
        }

        private NotificationIcon notificationIcon;
        public NotificationIcon NotificationIcon
        {
            get
            {
                return this.notificationIcon;
            }
            set
            {
                this.notificationIcon = value;
                this.RaisePropertyChangeEvent("NotificationIcon");
            }
        }

        public MessageBox()
        {
            InitializeComponent();
        }

         private ImageSource GetIconImage()
        {
            string text = null;
            BitmapImage result = null;
            switch (this.notificationIcon)
            {
                case NotificationIcon.Error:
                    {
                        text = "Error.png";
                        break;
                    }
                case NotificationIcon.Warning:
                    {
                        text = "Warning.png";
                        break;
                    }
                case NotificationIcon.Information:
                    {
                        text = "Information.png";
                        break;
                    }
                case NotificationIcon.Shield:
                    {
                        text = "Shield.png";
                        break;
                    }
            }
            if (text != null)
            {
                //string name = Assembly.GetExecutingAssembly().GetName().Name;
                string name = typeof(MessageBox).Assembly.GetName().Name;
                Uri uriSource = new Uri(string.Format(CultureInfo.CurrentUICulture, "/{0};component/Resources/{1}", new object[]
				{
					name, 
					text
				}), UriKind.Relative);
                result = new BitmapImage(uriSource);
            }
            return result;
        }
        
        private void RaisePropertyChangeEvent(string propertyName)
        {
            PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if (propertyChanged != null)
            {
                propertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }    
   
        public NotificationButtons Display()
        {
            this.DisplayHelper();
            return this.selectedButton;
        }

        public static NotificationButtons Show(string description, string title, object optionalContent, NotificationButtons buttons, NotificationIcon icon)
        {
            return new MessageBox
            {
                Description = description,
                Title = title,
                OptionalContent = optionalContent,
                NotificationButtons = buttons,
                NotificationIcon = icon
            }.Display();
        }

        private void DisplayHelper()
        {
            base.Owner = Application.Current.MainWindow;
            this.selectedButton = NotificationButtons.Cancel;
            base.Icon = Application.Current.MainWindow.Icon;
            this.ComposeUI();
            base.ShowDialog();
        }

        internal void ComposeUI()
        {
            this.SetWindowCloseButtonState();
            if (this.optionalContent == null)
            {
                this.optionalContentControl.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.optionalContentControl.Visibility = Visibility.Visible;
            }
            if (this.footer == null)
            {
                this.footerPanel.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.footerPanel.Visibility = Visibility.Visible;
            }
            ImageSource iconImage = this.GetIconImage();
            if (iconImage != null)
            {
                this.iconImageControl.Source = iconImage;
                this.iconImageControl.Visibility = Visibility.Visible;
            }
            else
            {
                if (this.NotificationIcon == NotificationIcon.Custom)
                {
                    this.iconImageControl.Visibility = Visibility.Visible;
                }
                else
                {
                    this.iconImageControl.Visibility = Visibility.Collapsed;
                }
            }
            this.closeButton.Visibility = (this.cancelButton.Visibility = (this.retryButton.Visibility = (this.noButton.Visibility = (this.yesButton.Visibility = (this.okButton.Visibility = Visibility.Collapsed)))));
            if ((this.notificationButtons & NotificationButtons.Close) == NotificationButtons.Close)
            {
                this.closeButton.Visibility = Visibility.Visible;
            }
            if ((this.notificationButtons & NotificationButtons.Cancel) == NotificationButtons.Cancel)
            {
                this.cancelButton.Visibility = Visibility.Visible;
            }
            if ((this.notificationButtons & NotificationButtons.Retry) == NotificationButtons.Retry)
            {
                this.retryButton.Visibility = Visibility.Visible;
            }
            if ((this.notificationButtons & NotificationButtons.No) == NotificationButtons.No)
            {
                this.noButton.Visibility = Visibility.Visible;
            }
            if ((this.notificationButtons & NotificationButtons.Yes) == NotificationButtons.Yes)
            {
                this.yesButton.Visibility = Visibility.Visible;
            }
            if ((this.notificationButtons & NotificationButtons.OK) == NotificationButtons.OK)
            {
                this.okButton.Visibility = Visibility.Visible;
            }
            Button button = this.okButton;
            if (this.defaultButton == NotificationButtons.Close)
            {
                button = this.closeButton;
            }
            else
            {
                if (this.defaultButton == NotificationButtons.Cancel)
                {
                    button = this.cancelButton;
                }
                else
                {
                    if (this.defaultButton == NotificationButtons.Retry)
                    {
                        button = this.retryButton;
                    }
                    else
                    {
                        if (this.defaultButton == NotificationButtons.No)
                        {
                            button = this.noButton;
                        }
                        else
                        {
                            if (this.defaultButton == NotificationButtons.Yes)
                            {
                                button = this.yesButton;
                            }
                            else
                            {
                                if (this.defaultButton == NotificationButtons.OK)
                                {
                                    button = this.okButton;
                                }
                            }
                        }
                    }
                }
            }
            button.IsDefault = true;
            button.Focus();
        }

        private void SetWindowCloseButtonState()
        {
            if (this.hwndSource == null)
            {
                return;
            }
            if (this.isCancelable)
            {
                IntPtr systemMenu = NativeMethods.GetSystemMenu(this.hwndSource.Handle, 0);
                NativeMethods.EnableMenuItem(systemMenu, 61536, 0);
                return;
            }
            IntPtr systemMenu2 = NativeMethods.GetSystemMenu(this.hwndSource.Handle, 0);
            NativeMethods.EnableMenuItem(systemMenu2, 61536, 1);
        }


        private void OnButtonClick(object sender, RoutedEventArgs e)
        {
            if (sender == this.closeButton)
            {
                this.selectedButton = NotificationButtons.Close;
            }
            else
            {
                if (sender == this.cancelButton)
                {
                    this.selectedButton = NotificationButtons.Cancel;
                }
                else
                {
                    if (sender == this.retryButton)
                    {
                        this.selectedButton = NotificationButtons.Retry;
                    }
                    else
                    {
                        if (sender == this.noButton)
                        {
                            this.selectedButton = NotificationButtons.No;
                        }
                        else
                        {
                            if (sender == this.yesButton)
                            {
                                this.selectedButton = NotificationButtons.Yes;
                            }
                            else
                            {
                                if (sender == this.okButton)
                                {
                                    this.selectedButton = NotificationButtons.OK;
                                }
                            }
                        }
                    }
                }
            }
            this.closeInvokedInternally = true;
            base.Close();
            this.closeInvokedInternally = false;
        }
        private void OnCloseButtonClick(object sender, RoutedEventArgs e)
        {
            this.closeInvokedInternally = true;
            base.Close();
            this.closeInvokedInternally = false;
        }
        protected override void OnClosing(CancelEventArgs e)
        {
            if (!this.closeInvokedInternally && !this.isCancelable)
            {
                e.Cancel = true;
            }
            base.OnClosing(e);
        }
        protected override void OnClosed(EventArgs e)
        {
            if (this.hwndSource != null)
            {
                this.hwndSource.Dispose();
                this.hwndSource = null;
            }
            base.OnClosed(e);
        }
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Cancel || e.Key == Key.Escape)
            {
                base.Close();
            }
            base.OnKeyDown(e);
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            this.hwndSource = (HwndSource)PresentationSource.FromVisual(this);
            this.SetWindowCloseButtonState();
            base.OnSourceInitialized(e);
        }
    }
}
